/*
 * Copyright (c) 2012-2013, Qualcomm Technologies, Inc.
 * All Rights Reserved.
 * Qualcomm Technologies Proprietary and Confidential.
 */

package com.android.hw.projectmenu;



import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceClickListener;
import android.util.Log;
import android.view.View;

public class ProjectMenu extends PreferenceActivity {

    private static final String KEY_UPDATE_DEVICE = "update_device";
    private Context mContext = null;
    private static boolean batteryLevelSuccess;
    private String TAG = "ProjectMenu";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        mContext = this;
        addPreferencesFromResource(R.xml.project_menu);
        batteryLeveltTest();
        PreferenceScreen updateDevicePreferenceScreen = (PreferenceScreen) findPreference(KEY_UPDATE_DEVICE);
        updateDevicePreferenceScreen
                .setOnPreferenceClickListener(new OnPreferenceClickListener() {

                    public boolean onPreferenceClick(Preference preference) {
                      /*  gaoshenpan 	0066982 fix start huawei otaUpdate begin*/
                        if (batteryLevelSuccess) {
                            otaUpdate();

                        } else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(
                                    mContext);
                            builder.setTitle(R.string.update_device)
                                    .setMessage(R.string.battery_toolow)
                                    .setCancelable(false)
                                    .setPositiveButton(
                                            R.string.yes,
                                            new DialogInterface.OnClickListener() {
                                                public void onClick(
                                                        DialogInterface dialog,
                                                        int id) {
                                                    dialog.cancel();
                                                }
                                            });
                            AlertDialog alert = builder.create();
                            alert.show();
                        }
                        /*  gaoshenpan 	0066982 fix start huawei otaUpdate end*/
                        return false;
                    }
                });
    }
    /**
     * add start huawei otaUpdate
     * gaoshenpan 15-08-05
     */
    private void otaUpdate(){
         Preference preference = getPreferenceScreen().findPreference("update_device");
         if (preference == null) {
             return ;
         }

         Intent intent = new Intent();
         intent.setAction("android.settings.SYSTEM_UPDATE_SETTINGS");
         if (intent != null) {
             // Find the activity that is in the system image
             PackageManager pm = this.getPackageManager();
             List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
             Log.d(TAG, "intent != null"+0);
             int listSize = list.size();
             for (int i = 0; i < listSize; i++) {
                 ResolveInfo resolveInfo = list.get(i);
                 Log.d(TAG, "intent != null"+"   i="+i);
                 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
                         != 0) {
                     Log.d(TAG, "intent != null"+1);
                     // Replace the intent with this specific activity
                     Intent intent1 = new Intent();
                     intent1.setClassName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
                      startActivity(intent1);
                     preference.setTitle(resolveInfo.loadLabel(pm));
                 }
             }
         }

    }
    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onResume()
     */
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        batteryLevelSuccess = false;
        batteryLeveltTest();
    }

    public void batteryLeveltTest() {
        // TODO Auto-generated method stub
        // start to test
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = mContext.registerReceiver(null, ifilter);
        // 当前剩余电量
        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        // 电量最大值
        int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        // 电量百分比
        float batteryPct = level / (float) scale;
        // if(mTestResult!=null)
        {
            if (batteryPct >= 0.2) {
                Log.d(TAG, "batteryLevelSuccess----->true");
                batteryLevelSuccess = true;
            } else {
                Log.d(TAG, "batteryLevelSuccess----->false");
                batteryLevelSuccess = false;
            }
        }
    }

    private void reboot(String reason) {
        Intent intent = new Intent(Intent.ACTION_REBOOT);
        intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
        intent.putExtra(Intent.EXTRA_REASON, reason);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
